/***********************************************************************
 * Module:  SPIVehicleTravelStrategy.java
 * Author:  Administrator
 * Purpose: Defines the Class SPIVehicleTravelStrategy
 ***********************************************************************/

package ishitong.vehicleManagement.service.biz.impl;

import ishitong.vehicle.common.service.biz.DrivingstrategySerivce;
import ishitong.vehicleManagement.model.VehicleTravelStrategy;
import ishitong.commons.pagination.Pagination;
import ishitong.commons.pagination.PaginationResult;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import ishitong.common.SysCodeType;
import ishitong.vehicle.common.model.Drivingstrategy;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ishitong.platform.common.page.Page;

/** @pdOid f05ac255-720d-4f44-a32a-e8a73e76508e */
@Service(value = "spiVehicleTravelStrategyService")
@Transactional
public class SPIVehicleTravelStrategyService {
   /** @pdOid 359bae84-54e8-4d27-9333-be38dfda7268
       @pdRoleInfo migr=yes name=SPISysCodeService assc=association16 */
   @Autowired
   private SPISysCodeService spiSysCodeService;
   /** @pdOid 69fb4344-078c-4baa-a8dd-e2b8aa83eba2
       @pdRoleInfo migr=yes name=DrivingstrategySerivce assc=association17 */
   @Autowired
   private DrivingstrategySerivce drivingstrategySerivce;
   
   /** @param drivingstrategy
    * @pdOid 61ab6b3b-3fe2-4e01-b4fe-d4fa3567098e */
   private VehicleTravelStrategy setVehicleTravelStrategy(Drivingstrategy drivingstrategy) {
    VehicleTravelStrategy vehicleTravelStrategy = null;
    if (drivingstrategy != null) { 
     vehicleTravelStrategy = new VehicleTravelStrategy();
   BeanUtils.copyProperties(drivingstrategy, vehicleTravelStrategy);
   vehicleTravelStrategy.setSignalLampStatus(spiSysCodeService.getSysCodeByCodeAndParentcode(String.valueOf(vehicleTravelStrategy.getSignalstate()),SysCodeType.SIGNAL_LAMP_STATUS));
   vehicleTravelStrategy.setTravelDirection(spiSysCodeService.getSysCodeByCodeAndParentcode(String.valueOf(vehicleTravelStrategy.getDirection()),SysCodeType.TRAVEL_DIRECTION));
    }
    return vehicleTravelStrategy;
     }
   
   /** @param id
    * @pdOid 164ef14b-fb1a-4b51-905a-32799624b0a9 */
   public VehicleTravelStrategy findById(String id) {
    Drivingstrategy drivingstrategy = new Drivingstrategy();
    drivingstrategy.setId(Long.valueOf(id));
    List<Drivingstrategy> lstDrivingstrategy = drivingstrategySerivce.getDrivingstrategyByCondition(drivingstrategy);
    if (lstDrivingstrategy.size() > 0) {
     drivingstrategy = lstDrivingstrategy.get(0);
     return setVehicleTravelStrategy(drivingstrategy);
    }
    return null;
   }
   
   /** @param vehicleTravelStrategy
    * @exception IllegalAccessException
    * @exception InvocationTargetException
    * @pdOid d4f2641f-bc58-4412-96e2-98c27349e1b7 */
   public List<VehicleTravelStrategy> findByQuery(VehicleTravelStrategy vehicleTravelStrategy) throws IllegalAccessException, InvocationTargetException {
   	Drivingstrategy drivingstrategy = new Drivingstrategy();
   	BeanUtils.copyProperties(vehicleTravelStrategy, drivingstrategy);
   	List<Drivingstrategy> lstDrivingstrategy = drivingstrategySerivce.getDrivingstrategyByCondition(drivingstrategy);
   	List<VehicleTravelStrategy> lstVehicleTravelStrategy = new ArrayList<VehicleTravelStrategy>();
   	for (Drivingstrategy ds : lstDrivingstrategy) {
   		lstVehicleTravelStrategy.add(setVehicleTravelStrategy(ds));
   	}
   	return lstVehicleTravelStrategy;
   }
   
   /** @param vehicleTravelStrategy 
    * @param pagination
    * @pdOid 61d6385f-398c-445d-a594-d1b2aa21d452 */
   public Page<VehicleTravelStrategy> findPage(VehicleTravelStrategy vehicleTravelStrategy, Pagination pagination) {
    Drivingstrategy drivingstrategy = new Drivingstrategy();
    BeanUtils.copyProperties(vehicleTravelStrategy, drivingstrategy);
    PaginationResult<Drivingstrategy> pr = drivingstrategySerivce.queryForPage(drivingstrategy, pagination);
    pagination = pr.getPagination();
    List<VehicleTravelStrategy> listVehicleTravelStrategy = new ArrayList<VehicleTravelStrategy>();
    for (Drivingstrategy ds: pr.getResults()) {
     listVehicleTravelStrategy.add(setVehicleTravelStrategy(ds));
    }
    Page<VehicleTravelStrategy> page = new Page<VehicleTravelStrategy>(pagination.getCurrentPage(),pagination.getPageSize(),pagination.getTotalRows(),listVehicleTravelStrategy);
      return page;
   }
   
   /** @param id
    * @pdOid fceba06d-78ee-4050-8444-c60d2e5f35bc */
   public void deleteVehicleTravelStrategyById(String id) {
    drivingstrategySerivce.deleteDrivingstrategyByID(Long.valueOf(id));
   }
   
   /** @param vehicleTravelStrategy
    * @pdOid 19c3eaac-5085-45ee-9561-d163b7694ed1 */
   public void saveVehicleTravelStrategy(VehicleTravelStrategy vehicleTravelStrategy) {
    Drivingstrategy drivingstrategy = new Drivingstrategy();
    BeanUtils.copyProperties(vehicleTravelStrategy, drivingstrategy); 
    if(vehicleTravelStrategy.getId() != null){
     drivingstrategySerivce.updateDrivingstrategyByID(drivingstrategy);
   	} else {
   	   drivingstrategySerivce.addDrivingstrategy(drivingstrategy);
   	}
   
   }

}